from rest_framework.viewsets import ViewSet
from rest_framework.decorators import action
from rest_framework.response import Response
from seckill.get_result import get_result
from .tasks import seckill_goods


class SeckillGoodsView(ViewSet):
    @action(methods=['GET'], detail=False)
    # 2 乐观锁秒杀--普通版
    # @transaction.atomic
    def seckill(self, request):
        task_id = seckill_goods.delay()
        print(task_id)
        return Response({'task_id': str(task_id)})

    @action(methods=['GET'], detail=False)
    def result(self, request):
        task_id = request.query_params.get('task_id')
        res = get_result(task_id)
        print(res)
        return Response({'result': res})
